<template>
  <div class="wrap">
    <el-form
      ref="ruleFormRef"
      style="max-width: 600px"
      :model="ruleForm"
      status-icon
      :rules="rules"
      label-width="auto"
      class="ruleForm-wrap"
    >
      <el-form-item label="场馆本地AI服务AppId">
        <el-select v-model="ruleForm.app_ai" placeholder="请选择">
          <el-option
            v-for="item in arr"
            :key="item.app_id"
            :label="item.app_name"
            :value="item.app_id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="场馆本地ALARM服务AppId">
        <el-select v-model="ruleForm.app_alarm" placeholder="请选择">
          <el-option
            v-for="item in arr"
            :key="item.app_id"
            :label="item.app_name"
            :value="item.app_id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="救生员手表AppId">
        <el-select v-model="ruleForm.app_rescuer" placeholder="请选择">
          <el-option
            v-for="item in arr"
            :key="item.app_id"
            :label="item.app_name"
            :value="item.app_id"
          />
        </el-select>
      </el-form-item>
      <el-form-item class="btn-wrap">
        <el-button type="primary" @click="submitForm(ruleFormRef)">
          设置
        </el-button>
        <el-button type="warning" plain @click="resetForm(ruleFormRef)"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { onMounted, reactive, ref } from 'vue'
import { ElMessage } from 'element-plus'
import { postData } from '@/http/request.js'
import { useRouter } from 'vue-router'
let router = useRouter()
let ruleFormRef = ref(null)
let app_id = ''
let arr = ref([])

const ruleForm = reactive({
  app_ai: '',
  app_alarm: '',
  app_rescuer: '',
})

const rules = reactive({
  app_ai: [{ required: false, message: '', trigger: 'blur' }],
  app_alarm: [{ required: false, message: '', trigger: 'blur' }],
  app_rescuer: [{ required: false, message: '', trigger: 'blur' }],
})

let get_app_name = () => {
  postData(
    '/dev/app/list',
    {
      dev_id: app_id ? app_id : '',
    },
    {
      timeout: 120000,
    }
  )
    .then((res) => {
      if (res.code == 0) {
        arr.value = res.datas.apps
      } else if (res.code == -56055) {
        return
      } else {
        ElMessage({
          type: 'error',
          message: 'error',
        })
      }
    })
    .catch((res) => {
      ElMessage({
        type: 'error',
        message: 'error',
      })
    })
}

let init_show = () => {
  postData('/manage/sys/setting/get', { fields: [] })
    .then((res) => {
      if (res.code == 0) {
        console.log(res)
        console.log(res)
        ruleForm.app_ai = res.datas.app_ai
        ruleForm.app_alarm = res.datas.app_alarm
        ruleForm.app_rescuer = res.datas.app_rescuer
        // let res_arr = res.data.app_rescuer
        // Object.assign(ruleForm, { ...res_arr })
      } else if (res.code == -56055) {
        return
      } else {
        ElMessage({
          type: 'error',
          message: 'error',
        })
      }
    })
    .catch((res) => {
      ElMessage({
        type: 'error',
        message: 'error',
      })
    })
}

const submitForm = async (formEl) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log(ruleForm.app_rescuer)
      console.log(ruleForm.app_ai)
      console.log(ruleForm.app_alarm)
      postData('/manage/sys/setting/set', {
        app_ai: ruleForm.app_ai,
        app_alarm: ruleForm.app_alarm,
        app_rescuer: ruleForm.app_rescuer,
      })
        .then((res) => {
          if (res.code == 0) {
            ElMessage({
              type: 'success',
              message: '设置成功',
            })
            // setTimeout(() => {
            //   router.push('/application')
            // }, 3000)
          } else {
            ElMessage({
              type: 'error',
              message: '设置失败',
            })
          }
        })
        .catch((err) => {
          console.log(err)
        })
    } else {
      console.log('error submit!', fields)
    }
  })
}

const resetForm = (formEl) => {
  if (!formEl) return
  formEl.resetFields()
}

onMounted(() => {
  get_app_name()
  init_show()
})
</script>
<style scope>
.wrap {
  margin: 20px 0 0 20px;
}
.btn-wrap {
  margin-top: 30px;
}
</style>
